package main

import (
	"fmt"
	"github.com/gorhill/cronexpr"
	"time"
)

func main() {
	var (
		expr *cronexpr.Expression
		err  error
		now  time.Time
		nextTime time.Time
	)

	// 每分钟执行
	//if expr,err = cronexpr.Parse("* * * * *");err!=nil{
	//	fmt.Println(err)
	//	return
	//}

	// 每5秒执行
	if expr, err = cronexpr.Parse("*/5 * * * * * *"); err != nil {
		fmt.Println(err)
	}

	now,_ = time.Parse("2006-1-2 15:04:05",time.Now().Format("2006-1-2 15:04:05"))
	nextTime = expr.Next(now) // 下次执行时间
	fmt.Println(now,nextTime)

	// 等待一段时间后执行
	time.AfterFunc(nextTime.Sub(now), func() {
		fmt.Println("hh",nextTime)
	})

	time.Sleep(10*time.Second)
}
